package org.ohos.sample.webview.home.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import org.ohos.sample.toolbar.Toolbar;
import org.ohos.sample.webview.ResourceTable;
import org.ohos.sample.webview.home.WebAbility;

public class HomeAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    private Toolbar mToolbar;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_home);
        mToolbar = Toolbar.builder(findComponentById(ResourceTable.Id_toolbar));
        initComponents();
        mToolbar.hideBack();
        mToolbar.setTitle("Home");
    }

    private void initComponents() {
        Component btn_webview_load_url = findComponentById(ResourceTable.Id_btn_webview_load_url);
        Component btn_webview_load_raw = findComponentById(ResourceTable.Id_btn_webview_load_raw);
        Component btn_webview_load_storage = findComponentById(ResourceTable.Id_btn_webview_load_storage);
        Component btn_webview_load_harmonyos = findComponentById(ResourceTable.Id_btn_webview_load_harmonyos);
        Component btn_webview_load_raw_home = findComponentById(ResourceTable.Id_btn_webview_load_raw_home);
        //------------------------------------------------------------------
        btn_webview_load_url.setClickedListener(this);
        btn_webview_load_raw.setClickedListener(this);
        btn_webview_load_storage.setClickedListener(this);
        btn_webview_load_harmonyos.setClickedListener(this);
        btn_webview_load_raw_home.setClickedListener(this);
    }

    private void startAbilitySlice(Class<?> cls) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder().withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(cls)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        int id = component.getId();
        if(id==ResourceTable.Id_btn_webview_load_url){
            WebAbility.start(getAbility(),"http://elemefe.github.io/mint-ui/#/");
        }else if(id==ResourceTable.Id_btn_webview_load_raw){
            //一：加载资源文件 resources/rawfile/example.html
            String rawUrl="https://example.com/rawfile/web/index.html";
            WebAbility.start(getAbility(),rawUrl);

            //二：加载资源文件 dataability://org.ohos.sample.data.DataAbility
           // final String URL_LOCAL = "dataability://org.ohos.sample.data.DataAbility/resources/rawfile/web/index.html";

        }else if(id==ResourceTable.Id_btn_webview_load_storage){
            //需要自己把HTML文件拷贝到设备存储开中.
            // 加载本地文件 /data/data/com.example.dataability/files/example.html
//            String storageUrl="https://example.com/local/web/index.html";
//            WebAbility.start(getAbility(),storageUrl);
        }else if(id==ResourceTable.Id_btn_webview_load_harmonyos){
            String url="https://developer.harmonyos.com";
            WebAbility.start(getAbility(),url);
        }else if(id==ResourceTable.Id_btn_webview_load_raw_home){
            String rawUrl="https://example.com/rawfile/web/index.html#/tabbar";
            WebAbility.start(getAbility(),rawUrl);
        }
    }
}